package mmc.objects;

import static org.lwjgl.opengl.ARBPointParameters.*;
import static org.lwjgl.opengl.ARBPointSprite.GL_POINT_SPRITE_ARB;
import static org.lwjgl.opengl.GL11.*;
import mmc.EngineObject;
import mmc.Scene;
import mmc.resource.TextureLoader;

@SuppressWarnings("serial")
public class ParticleTest extends EngineObject{
	
	public ParticleTest(float x, float y, float z) {
		super(x, y, z);
		health = 1e-9f;
	}
	
	
	transient int count = 1000;
	transient float[] x;
	transient float[] y;
	transient float[] z;
	transient float[] vx;
	transient float[] vy;
	transient float[] vz;
	
	public void initOnClient(Scene scene){
		x = new float[count];
		y = new float[count];
		z = new float[count];
		vy = new float[count];
		vz = new float[count];
		vx = new float[count];
		
		for(int i=0;i<count;i++){
			x[i] = super.x;
			y[i] = super.y;
			z[i] = super.z;
		}
	}

	@Override
	public void render(EngineObject observer) {
		float size = 10;
		
		glColor4f(1,1,1,1);
		glEnable(GL_TEXTURE);
		glDisable(GL_LIGHTING);
		
		glPointParameterfARB(GL_POINT_SIZE_MIN_ARB, 1e-9f);
        glPointParameterfARB(GL_POINT_SIZE_MAX_ARB, 1e9f);
        glPointParameterfARB(org.lwjgl.opengl.ARBPointParameters.GL_POINT_DISTANCE_ATTENUATION_ARB,1);
        glPointSize(size);
        glEnable(GL_POINT_SPRITE_ARB);
        
        TextureLoader.load("mmc/resource/data/Smoke.png").bind();
        
        glDepthMask(false);
        glBegin(GL_POINTS);
        glTexCoord2f(0.5f, 0.5f);
        for(int i=0;i<count;i++)
        	glVertex3f(x[i],y[i],z[i]);
        glEnd();
        glDepthMask(true);
        
        glDisable(GL_POINT_SPRITE_ARB);
        glEnable(GL_LIGHTING);
	}

	@Override
	public void update(float dt) {
		for(int i=0;i<count;i++){
			x[i] += vx[i]*dt;
			y[i] += vy[i]*dt;
			z[i] += vz[i]*dt;
			
			if(Math.random()<1e-3 || y[i] > super.y + 12){
				x[i] = super.x;
				y[i] = super.y;
				z[i] = super.z;
				vx[i] = (float)(2*Math.random()-1)*2;
				vy[i] = 10;
				vz[i] = (float)(2*Math.random()-1)*2;
			}
		}
	}

	@Override
	public boolean copyTo(EngineObject o) {
		o.x = super.x;
		o.y = super.y;
		o.z = super.z;
		o.health = super.health;
		
		return true;
	}
}
